package ht.android.sqlite;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class Buoi12SQLiteActivity extends Activity implements OnClickListener {
	public static final String strTable = "tblAmingo";

	Button btnCreate;
	Button btnQuery;
	Button btnInsert;
	Button btnUpdate;
	Button btnDelete;
	Button btnDrop;
	Button btnExit;

	EditText txtName;
	EditText txtPhone;

	TableLayout tblQuery;

	ManageTableAmingo objDB;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		btnCreate = (Button) findViewById(R.id.btnCreate);
		btnCreate.setOnClickListener(this);

		btnInsert = (Button) findViewById(R.id.btnInsert);
		btnInsert.setOnClickListener(this);

		btnQuery = (Button) findViewById(R.id.btnQuery);
		btnQuery.setOnClickListener(this);

		btnUpdate = (Button) findViewById(R.id.btnUpdate);
		btnUpdate.setOnClickListener(this);

		btnDelete = (Button) findViewById(R.id.btnDelete);
		btnDelete.setOnClickListener(this);

		btnDrop = (Button) findViewById(R.id.btnDrop);
		btnDrop.setOnClickListener(this);

		btnExit = (Button) findViewById(R.id.btnExit);
		btnExit.setOnClickListener(this);

		txtName = (EditText) findViewById(R.id.txtName);
		txtPhone = (EditText) findViewById(R.id.txtPhone);

		objDB = new ManageTableAmingo(this);

		tblQuery = (TableLayout) findViewById(R.id.tblQuery);
	}

	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.btnCreate:
			objDB.createTable(strTable);

			break;
		case R.id.btnDelete:

			break;
		case R.id.btnDrop:
			objDB.dropTable(strTable);

			break;
		case R.id.btnExit:
			finish();

			break;
		case R.id.btnInsert:
			objDB.insertData(txtName.getText().toString(), txtPhone.getText()
					.toString());

			break;
		case R.id.btnQuery:
			displayData(objDB.queryTable("", ""));

			break;
		case R.id.btnUpdate:

			break;

		}
	}

	private void displayData(String[] queryTable) {
		// TODO Auto-generated method stub
		TableRow tbl;
		TextView lblID;
		TextView lblName;
		TextView lblPhone;

		int intNumRecord = queryTable.length / 3;
		int intIndex = 0;
		while (intNumRecord > 0) {
			tbl = new TableRow(this);
			tbl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT));
			lblID = new TextView(this);
			lblID.setText(queryTable[intIndex]);
			tbl.addView(lblID);

			intIndex++;
			lblName = new TextView(this);
			lblName.setText(queryTable[intIndex]);
			tbl.addView(lblName);

			intIndex++;
			lblPhone = new TextView(this);
			lblPhone.setText(queryTable[intIndex]);
			tbl.addView(lblPhone);

			tblQuery.addView(tbl);

			intIndex++;
			intNumRecord--;
		}

	}

	private void displayData(Cursor objCur) {
		// TODO Auto-generated method stub

	}
}